/*
 * @author larry
 **/
!function(){

	// local variable
	var apiIndex,
		apiStore = {},
		sendMsg = chrome.runtime.sendMessage,
		inspectedWindow = chrome.devtools.inspectedWindow;

	// 初始化CodeMirror
	var editor = CodeMirror.fromTextArea($('#codeEditor')[0], {
		mode: "javascript",
		theme : "monokai",
		indentUnit : 4,
		indentWithTabs : true,
		lineNumbers: true,
	    lineWrapping: true
	});

	// executeScript 
	function executeScript(){
		sendMsg({
			'action' : 'executeScript',
		    tabId: inspectedWindow.tabId,
		    code: editor.getValue()
		});
		//sendMsg(1111);
	}

	// 创建apiStore
	$.getJSON('../api/zepto.json', function (indexData) {
		apiIndex = lunr(function () {
			this.field('name', {boost: 1000});
			this.field('category', {boost: 100});
			this.field('category_desc', {boost: 10});
			this.field('method_details_text', {boost: 1});
			this.ref('name');
			//sendMsg(indexData);

		});
		$.each(indexData,function( idx , row){
			apiIndex.add(row);
			row.lib = 'Zepto.js';
			apiStore[row.name] = row;
		});
		//sendMsg(apiStore);
		//sendMsg(apiIndex);
		//index.add();
	});

	// api 查询部分
	var keyword = $('#keyword'),
		api_result = $('#api-result'),
		api_query = $('#api-query').modal({
			show : false // TODO: 初始化时不显示
		}).on('shown.bs.modal', function () {
			keyword.focus();
		});

	// shortcut key & mouse - event binding
	keyword.keyup(function(){
		var result = apiIndex.search(keyword.val());//,
			//fragment = document.createDocumentFragment();

		//sendMsg(result);
		api_result.empty();
		//api_result.css('display' , 'block');
		$.each(result , function(index , r){
			var li = document.createElement('li'),
				record = apiStore[r.ref],
				html = [];
			li.className = 'list-group-item';
			html.push('<article class="method"><div class="method-info">');
			html.push('<span class="label label-success">');
			html.push('Category&nbsp;:&nbsp;');
			html.push(record['category']);
			html.push('</span>');
			html.push('<span class="label label-primary">');
			html.push('Lib&nbsp;:&nbsp;');
			html.push(record['lib']);
			html.push('</span>');
			html.push('</div>');
			html.push(record['method_details_html']);
			html.push('</article>');
			li.innerHTML = html.join('');
			//sendMsg(record);
			api_result[0].appendChild(li);
		});
		//api_result[0].appendChild(fragment);
		//sendMsg([keyword.val(),result]);	
	});

	// 代码执行按钮
	$('#executeBtn').click(function(evt){
		// Relay the tab ID to the background page
		executeScript();
		//sendMsg(111);

		//chrome.devtools.inspectedWindow.eval("window.name=1" , function(){
		//	console.log(arguments);
		//});
		//chrome.extension.sendMessage(editor.getValue() ,function(){
		//	console.log('send ok!');
		//})
		//chrome.devtools.inspectedWindow
		//chrome.tabs.executeScript({
		//	code : editor.getValue()
		//}, function(result){
		//	console.log(result);
		//});
	});

	editor.addKeyMap({
		'Ctrl-E' : function(cm){
			executeScript();
		}
	});

	$(document).keypress(function(evt){
		if(evt.which == 5){ // ctrl + e
			executeScript();
		}
		//sendMsg(evt.which);
	}).keypress(function(evt){
		console.log('47');
		if(evt.which == 47){
			//sendMsg(evt.which)
			api_query.modal('show');
		}
	});

	// Common Util
}()